package com.zy.control;

import com.fasterxml.jackson.databind.JsonNode;
import com.networknt.schema.JsonSchema;
import com.networknt.schema.ValidationMessage;
import com.zy.util.BaseJsonSchemaValidatorUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Set;

/**
 * @author ZY
 * @describe
 * @date 2020/7/29
 */
@RestController
@RequestMapping("/schema")
@Slf4j
@ApiIgnore
public class SchemaController {

    @SneakyThrows
    @PostMapping("/v1/validate")
    public String schemaValidate(String json, String schemaStr) {

        JsonNode schemaNode = BaseJsonSchemaValidatorUtils.getJsonNodeFromStringContent(schemaStr);

        JsonSchema schema = BaseJsonSchemaValidatorUtils.getJsonSchemaFromJsonNodeAutomaticVersion(schemaNode);
        JsonNode node = BaseJsonSchemaValidatorUtils.getJsonNodeFromStringContent(json);
        Set<ValidationMessage> errors = schema.validate(node);

        errors.forEach((s)->{
            log.info("code:{} ** path:{} ** msg:{} ** detail:{} ** type:{} ** args:{}",s.getCode(),s.getPath(),s.getMessage(),s.getDetails(),s.getType(),s.getArguments());

            log.info("all : {}",s);
        });
        return "schema validate suc!!";
    }



}
